﻿@using Ornament.MemberShip
@model Ornament.MemberShip.User
@{
    ViewBag.Title = "分配角色:" + this.Model.Name;
    Layout = "~/Views/Shared/App.cshtml";

    Html.BeginForm();
    <div>
        <button type="submit" id="submit">
            @Resources.Basic.Save</button>
        <button type="button" id="cancel">
            @Resources.Basic.Cancel</button>
    </div>   
    
    <input type="hidden"  value="@Model.LoginId" name="LoginId"/>
    <div id="UserGroups" class="cssform">
        <h3>
            @Resources.MemberShip.UserGroup</h3>
        @foreach (var ug in Ornament.Web.OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateUserGroupDao().GetAll())
        {
            <p>
                <label for="@ug.Id.ToString("N")">@ug.Name</label>
                <input id="@ug.Id.ToString("N")" name="UserGroups"  value="@ug.Id" type="checkbox" checked="@this.Model.GetUserGroups().Contains(ug)"/>
            </p>
        }
    </div>
    <div id="Roles" class="cssform">
        <h3>
            @Resources.MemberShip.Role</h3>
        @foreach (var role in Ornament.Web.OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreateRoleDao().GetAll())
        {
            var check = this.Model.InRole(role, false) ? "checked=\"checked\"" : "";
            <p>
                <label for="@role.Id.ToString("N")">@role.Name</label>
                <input id="@role.Id.ToString("N")" name="Roles"  value="@role.Id"  type="checkbox" @check/>
            </p>
        }
    </div>
      
    <div id="permissionPanel">
        <h3>
            @Resources.MemberShip.Permission</h3>
        @{var permissions = from a in Ornament.Web.OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().Permissions
                            orderby a.Name
                            select a;
          object resourceId = null;
          foreach (var bag in permissions)
          {
              if (resourceId != bag.Resource)
              {
            <h4>@bag.Resource.ToString()</h4>
           resourceId = bag.Resource;
              }
            <div id="@bag.Id.ToString("N")">@bag.Name</div>            
          }
                          
        }
    </div>
    <div style="clear: both">
    </div>
    Html.EndForm();
}
@section header{
    <style type="text/css">
        #Roles, #UserGroups, #permissionPanel
        {
            float: left;
            width: 250px;
        }
        .highlight
        {
            color: blue;
        }
    </style>
    <script type="text/javascript">
    var userGroupMap=@MvcHtmlString.Create(Qi.Web.JsonHelper.ToJson(ViewData["userGroupRoleMap"]));
    var roleGroupMap=@MvcHtmlString.Create(Qi.Web.JsonHelper.ToJson(ViewData["rolePermissionMap"]));
        $(document).ready(function () {
           
            $("#cancel").button().click(function () { location = '@Url.Action("Index")'; });
            $("#submit").button();
            $("#UserGroups input").change(function(){
                $("#Roles label").removeClass("highlight");
                $("#UserGroups input:checked").each(function(){
                    var groupId=$(this).attr("id");
                    $.each(userGroupMap[groupId],function(){                
                        $("#Roles label[for="+this+"]").addClass("highlight");
                    });
                });
            }).change();

            $("#Roles input").change(function(){
                $("#permissionPanel div").removeClass("highlight");
                $("#Roles input:checked").each(function(){
                    var roleId=$(this).attr("id");
                    $.each(roleGroupMap[roleId],function(){                
                        $("#permissionPanel div[id="+this+"]").addClass("highlight");
                    });
                });
            }).change();
            
        });
    </script>
}
@section PageTitle{
    @Resources.MemberShip.AssignRoles:@this.Model.LoginId
}
